//
//  RegisterViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/12.
//  Copyright © 2016年 revocher. All rights reserved.
//
//
#import "RegisterViewController.h"
#import <AVOSCloud.h>
#import "UIImageView+WebCache.h"
@interface RegisterViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *photoImageView;
@property (strong, nonatomic) IBOutlet UITextField *nickNameField;
@property (strong, nonatomic) IBOutlet UITextField *registerField;
@property (strong, nonatomic) IBOutlet UITextField *securityCodeField;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;
@property(strong,nonatomic) UIImage * myImage;


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.photoImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickerImage:)];
    [self.photoImageView addGestureRecognizer:tapGR];

    
    
}
-(void)pickerImage:(UITapGestureRecognizer *)sender
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开相机拍照
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        pickerVC.allowsEditing = YES;
        pickerVC.delegate = self;
        [self presentViewController:pickerVC animated:YES completion:nil];
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册选择照片添加为头像
        
        UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
        pickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pickerVC.allowsEditing = YES;
        pickerVC.delegate = self;
        
        [self presentViewController:pickerVC animated:YES completion:nil];
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertVC addAction:cameraAction];
    [alertVC addAction:photoAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.photoImageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 获取验证码点击事件
- (IBAction)securityCodeBtnClickedAction:(id)sender {
    //注册前先查找
    AVQuery *query = [AVQuery queryWithClassName:@"_User"];
    [query whereKey:@"mobilePhoneNumber" equalTo:self.registerField.text];
    [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        if (object == nil) {
            AVUser *user =[AVUser user];
            user.username = self.nickNameField.text;
            user.password = self.passwordField.text;
            user.mobilePhoneNumber = self.registerField.text;
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"发送成功");
                }else{
                    NSLog(@"%@",error);
                }
            }];
        }
    }];

    
    
    
}
#pragma mark --注册按钮点击事件
- (IBAction)registerBtnClickedAction:(id)sender {
    [AVUser verifyMobilePhone:self.securityCodeField.text withBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSMutableArray *arr = [NSMutableArray array];
            
            
            NSData *data = UIImagePNGRepresentation(self.photoImageView.image);
            NSLog(@"%@++++++++++",[AVUser currentUser].mobilePhoneNumber);
            AVFile *file = [AVFile fileWithName:[NSString stringWithFormat:@"%@.png",self.nickNameField.text]data:data];
            [file saveInBackground];
        
            
            
            UIAlertController * alertControlller = [UIAlertController alertControllerWithTitle:@"提示" message:@"恭喜您注册成功~请点击确定返回登陆界面~" preferredStyle:    UIAlertControllerStyleAlert];
            UIAlertAction *  cancelAction = [UIAlertAction actionWithTitle:@"确定" style:  UIAlertActionStyleDefault  handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            

            }];
            [alertControlller addAction: cancelAction];
            [self.navigationController popToRootViewControllerAnimated:YES];
        
        }else{
            NSLog(@"%@",error);
        }
    }];


}




/*
#pragma mark - Navigation

 In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     Get the new view controller using [segue destinationViewController].
     Pass the selected object to the new view controller.
}
*/

@end
